(function(e,t){typeof exports==="object"&&typeof module!=="undefined"?module.exports=t():typeof define==="function"&&define.amd?define(t):(e=e||self,e.Compressor=t())})(this,function(){"use strict";function e(e,t){if(!(e instanceof t)){throw new TypeError("Cannot call a class as a function")}}function t(e,t){for(var r=0;r<t.length;r++){var a=t[r];a.enumerable=a.enumerable||false;a.configurable=true;if("value"in a)a.writable=true;Object.defineProperty(e,a.key,a)}}function r(e,r,a){if(r)t(e.prototype,r);if(a)t(e,a);return e}function a(e,t,r){if(t in e){Object.defineProperty(e,t,{value:r,enumerable:true,configurable:true,writable:true})}else{e[t]=r}return e}function i(){i=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var a in r){if(Object.prototype.hasOwnProperty.call(r,a)){e[a]=r[a]}}}return e};return i.apply(this,arguments)}function n(e){for(var t=1;t<arguments.length;t++){var r=arguments[t]!=null?arguments[t]:{};var i=Object.keys(r);if(typeof Object.getOwnPropertySymbols==="function"){i=i.concat(Object.getOwnPropertySymbols(r).filter(function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable}))}i.forEach(function(t){a(e,t,r[t])})}return e}function o(e,t){return t={exports:{}},e(t,t.exports),t.exports}var l=o(function(e){if(typeof window==="undefined"){return}(function(t){var r=t.HTMLCanvasElement&&t.HTMLCanvasElement.prototype;var a=t.Blob&&function(){try{return Boolean(new Blob)}catch(e){return false}}();var i=a&&t.Uint8Array&&function(){try{return new Blob([new Uint8Array(100)]).size===100}catch(e){return false}}();var n=t.BlobBuilder||t.WebKitBlobBuilder||t.MozBlobBuilder||t.MSBlobBuilder;var o=/^data:((.*?)(;charset=.*?)?)(;base64)?,/;var l=(a||n)&&t.atob&&t.ArrayBuffer&&t.Uint8Array&&function(e){var t,r,l,f,s,u,c,h,v;t=e.match(o);if(!t){throw new Error("invalid data URI")}r=t[2]?t[1]:"text/plain"+(t[3]||";charset=US-ASCII");l=!!t[4];f=e.slice(t[0].length);if(l){s=atob(f)}else{s=decodeURIComponent(f)}u=new ArrayBuffer(s.length);c=new Uint8Array(u);for(h=0;h<s.length;h+=1){c[h]=s.charCodeAt(h)}if(a){return new Blob([i?c:u],{type:r})}v=new n;v.append(u);return v.getBlob(r)};if(t.HTMLCanvasElement&&!r.toBlob){if(r.mozGetAsFile){r.toBlob=function(e,t,a){var i=this;setTimeout(function(){if(a&&r.toDataURL&&l){e(l(i.toDataURL(t,a)))}else{e(i.mozGetAsFile("blob",t))}})}}else if(r.toDataURL&&l){r.toBlob=function(e,t,r){var a=this;setTimeout(function(){e(l(a.toDataURL(t,r)))})}}}if(e.exports){e.exports=l}else{t.dataURLtoBlob=l}})(window)});var f=function e(t){if(typeof Blob==="undefined"){return false}return t instanceof Blob||Object.prototype.toString.call(t)==="[object Blob]"};var s={strict:true,checkOrientation:true,maxWidth:Infinity,maxHeight:Infinity,minWidth:0,minHeight:0,width:undefined,height:undefined,quality:.8,mimeType:"auto",convertSize:5e6,beforeDraw:null,drew:null,success:null,error:null};var u=typeof window!=="undefined";var c=u?window:{};var h=Array.prototype.slice;function v(e){return Array.from?Array.from(e):h.call(e)}var d=/^image\/.+$/;function m(e){return d.test(e)}function b(e){var t=m(e)?e.substr(6):"";if(t==="jpeg"){t="jpg"}return".".concat(t)}var y=String.fromCharCode;function p(e,t,r){var a="";var i;r+=t;for(i=t;i<r;i+=1){a+=y(e.getUint8(i))}return a}var g=c.btoa;function w(e,t){var r=[];var a=8192;var i=new Uint8Array(e);while(i.length>0){r.push(y.apply(null,v(i.subarray(0,a))));i=i.subarray(a)}return"data:".concat(t,";base64,").concat(g(r.join("")))}function U(e){var t=new DataView(e);var r;try{var a;var i;var n;if(t.getUint8(0)===255&&t.getUint8(1)===216){var o=t.byteLength;var l=2;while(l+1<o){if(t.getUint8(l)===255&&t.getUint8(l+1)===225){i=l;break}l+=1}}if(i){var f=i+4;var s=i+10;if(p(t,f,4)==="Exif"){var u=t.getUint16(s);a=u===18761;if(a||u===19789){if(t.getUint16(s+2,a)===42){var c=t.getUint32(s+4,a);if(c>=8){n=s+c}}}}}if(n){var h=t.getUint16(n,a);var v;var d;for(d=0;d<h;d+=1){v=n+d*12+2;if(t.getUint16(v,a)===274){v+=8;r=t.getUint16(v,a);t.setUint16(v,1,a);break}}}}catch(e){r=1}return r}function B(e){var t=0;var r=1;var a=1;switch(e){case 2:r=-1;break;case 3:t=-180;break;case 4:a=-1;break;case 5:t=90;a=-1;break;case 6:t=90;break;case 7:t=90;r=-1;break;case 8:t=-90;break;default:}return{rotate:t,scaleX:r,scaleY:a}}var k=/\.\d*(?:0|9){12}\d*$/;function x(e){var t=arguments.length>1&&arguments[1]!==undefined?arguments[1]:1e11;return k.test(e)?Math.round(e*t)/t:e}var M=c.ArrayBuffer,j=c.FileReader;var O=c.URL||c.webkitURL;var A=/\.\w+$/;var T=c.Compressor;var R=function(){function t(r,a){e(this,t);this.file=r;this.image=new Image;this.options=n({},s,a);this.aborted=false;this.result=null;this.init()}r(t,[{key:"init",value:function e(){var t=this;var r=this.file,a=this.options;if(!f(r)){this.fail(new Error("The first argument must be a File or Blob object."));return}var n=r.type;if(!m(n)){this.fail(new Error("The first argument must be an image File or Blob object."));return}if(!O||!j){this.fail(new Error("The current browser does not support image compression."));return}if(!M){a.checkOrientation=false}if(O&&!a.checkOrientation){this.load({url:O.createObjectURL(r)})}else{var o=new j;var l=a.checkOrientation&&n==="image/jpeg";this.reader=o;o.onload=function(e){var a=e.target;var o=a.result;var f={};if(l){var s=U(o);if(s>1||!O){f.url=w(o,n);if(s>1){i(f,B(s))}}else{f.url=O.createObjectURL(r)}}else{f.url=o}t.load(f)};o.onabort=function(){t.fail(new Error("Aborted to read the image with FileReader."))};o.onerror=function(){t.fail(new Error("Failed to read the image with FileReader."))};o.onloadend=function(){t.reader=null};if(l){o.readAsArrayBuffer(r)}else{o.readAsDataURL(r)}}}},{key:"load",value:function e(t){var r=this;var a=this.file,i=this.image;i.onload=function(){r.draw(n({},t,{naturalWidth:i.naturalWidth,naturalHeight:i.naturalHeight}))};i.onabort=function(){r.fail(new Error("Aborted to load the image."))};i.onerror=function(){r.fail(new Error("Failed to load the image."))};i.alt=a.name;i.src=t.url}},{key:"draw",value:function e(t){var r=this;var a=t.naturalWidth,i=t.naturalHeight,n=t.rotate,o=n===void 0?0:n,f=t.scaleX,s=f===void 0?1:f,u=t.scaleY,c=u===void 0?1:u;var h=this.file,v=this.image,d=this.options;var b=document.createElement("canvas");var y=b.getContext("2d");var p=a/i;var g=Math.abs(o)%180===90;var w=Math.max(d.maxWidth,0)||Infinity;var U=Math.max(d.maxHeight,0)||Infinity;var B=Math.max(d.minWidth,0)||0;var k=Math.max(d.minHeight,0)||0;var M=Math.max(d.width,0)||a;var j=Math.max(d.height,0)||i;if(g){var O=[U,w];w=O[0];U=O[1];var A=[k,B];B=A[0];k=A[1];var T=[j,M];M=T[0];j=T[1]}if(w<Infinity&&U<Infinity){if(U*p>w){U=w/p}else{w=U*p}}else if(w<Infinity){U=w/p}else if(U<Infinity){w=U*p}if(B>0&&k>0){if(k*p>B){k=B/p}else{B=k*p}}else if(B>0){k=B/p}else if(k>0){B=k*p}if(j*p>M){j=M/p}else{M=j*p}M=Math.floor(x(Math.min(Math.max(M,B),w)));j=Math.floor(x(Math.min(Math.max(j,k),U)));var R=-M/2;var E=-j/2;var L=M;var I=j;if(g){var C=[j,M];M=C[0];j=C[1]}b.width=M;b.height=j;if(!m(d.mimeType)){d.mimeType=h.type}var D="transparent";if(h.size>d.convertSize&&d.mimeType==="image/png"){D="#fff";d.mimeType="image/jpeg"}y.fillStyle=D;y.fillRect(0,0,M,j);if(d.beforeDraw){d.beforeDraw.call(this,y,b)}if(this.aborted){return}y.save();y.translate(M/2,j/2);y.rotate(o*Math.PI/180);y.scale(s,c);y.drawImage(v,R,E,L,I);y.restore();if(d.drew){d.drew.call(this,y,b)}if(this.aborted){return}var H=function e(t){if(!r.aborted){r.done({naturalWidth:a,naturalHeight:i,result:t})}};if(b.toBlob){b.toBlob(H,d.mimeType,d.quality)}else{H(l(b.toDataURL(d.mimeType,d.quality)))}}},{key:"done",value:function e(t){var r=t.naturalWidth,a=t.naturalHeight,i=t.result;var n=this.file,o=this.image,l=this.options;if(O&&!l.checkOrientation){O.revokeObjectURL(o.src)}if(i){if(l.strict&&i.size>n.size&&l.mimeType===n.type&&!(l.width>r||l.height>a||l.minWidth>r||l.minHeight>a)){i=n}else{var f=new Date;i.lastModified=f.getTime();i.lastModifiedDate=f;i.name=n.name;if(i.name&&i.type!==n.type){i.name=i.name.replace(A,b(i.type))}}}else{i=n}this.result=i;if(l.success){l.success.call(this,i)}}},{key:"fail",value:function e(t){var r=this.options;if(r.error){r.error.call(this,t)}else{throw t}}},{key:"abort",value:function e(){if(!this.aborted){this.aborted=true;if(this.reader){this.reader.abort()}else if(!this.image.complete){this.image.onload=null;this.image.onabort()}else{this.fail(new Error("The compression process has been aborted."))}}}}],[{key:"noConflict",value:function e(){window.Compressor=T;return t}},{key:"setDefaults",value:function e(t){i(s,t)}}]);return t}();return R});
//# sourceMappingURL=image-compressor.map.js